package cn.powerchaintech.common.task;

import org.springframework.scheduling.support.CronExpression;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

public abstract class CronTask  extends TaskExecutor {
    public CronTask(String taskName, String cronExpression) {
        super(taskName, cronExpression);
    }

    @Override
    public TaskType taskType() {
        return TaskType.CRON;
    }

    @Override
    public void notifySuccess() {

    }

    @Override
    public List<LocalDateTime> computeFireTimesBetween(LocalDateTime from, LocalDateTime to) {
        CronExpression parse = CronExpression.parse(getCronExpression());
        List<LocalDateTime> lst = new ArrayList<>();
        LocalDateTime next = parse.next(from);
        while(next != null && next.isBefore(to)) {
            lst.add(next);
        }
        return lst;
    }
}
